package com.sinoglobal.sinostore.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import com.lidroid.xutils.BitmapUtils;
import com.sinoglobal.sinostore.activity.TurntableImageActivity;
import com.sinoglobal.sinostore.bean.ImageVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.BitmapHelp;

public class GoodsBannerPagerAdapter extends PagerAdapter {
	private ArrayList<ImageVo> data; // 存放广告图片的列表
	Activity activity;
	BitmapUtils bitmapUtils;
	SlideLeftListener slideLeftListener;
	private List<View>views=new ArrayList<View>();
	public List<View> getViews() {
		return views;
	}
	public ArrayList<ImageVo> getData() {
		return data;
	}
	public void setSlideLeftListener(SlideLeftListener slideLeftListener) {
		this.slideLeftListener = slideLeftListener;
	}
	public GoodsBannerPagerAdapter(Activity activity,ArrayList<ImageVo> data) {
		super();
		this.activity = activity;
		this.data = data;
		bitmapUtils= BitmapHelp.getBitmapUtils(activity);
		for (int i = 0; i < data.size(); i++) {
			final ImageView iv=new ImageView(activity);
			bitmapUtils.display(iv, Constants.PIC_HOST+data.get(i).getUrl());
			views.add(iv);
		}
	}
	
	public void setData(ArrayList<ImageVo> list) {
		this.data = list;
		notifyDataSetChanged();
	}


	// 代表的是当前传进来的对象，是不是要在我当前页面显示的
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		//Auto-generated method stub
		return arg0 == arg1;// 如果当前显示的View跟你传进来的是同一个View,说明就是要显示的 view
	}

	@Override
	// 图片的数量
	public int getCount() {
		//Auto-generated method stub
		return data == null ? 0 : data.size();
	}

	/**
	 * 移除当前一张图片
	 * */
	@Override
	public void destroyItem(View view, int position, Object object) {
        if (object instanceof View &&view instanceof ViewGroup) {
			View currentView=(View)object;
			((ViewGroup) view).removeView(currentView);
		}
	}

	/**
	 * 添加一张图片
	 * */
	@Override
	public Object instantiateItem(View view, final int position) {
		ImageView iv=(ImageView) views.get(position);
		if (view instanceof ViewGroup) {
			((ViewGroup) view).addView(iv);
		}
		clickLookBigPic(position,iv);
		return iv;
	}
	
	/**点击看大图**/
	private void clickLookBigPic(final int position, ImageView iv) {
		iv.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(null != data && data.size()>0){
					Intent intent = new Intent(activity, TurntableImageActivity.class);
					intent.putExtra("imgurls", data);
					intent.putExtra("index", position);
					intent.putExtra("type", 1);
					activity.startActivity(intent);
				}
			}
		});
	}
	
	@Override
	public void finishUpdate(View arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void restoreState(Parcelable arg0, ClassLoader arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Parcelable saveState() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void startUpdate(View arg0) {
		// TODO Auto-generated method stub
		
	}
	public interface SlideLeftListener{
		void slideLeft();
	}
	private View mCurrentView;
    
//    @Override
    public void setPrimaryItem(View container, int position, Object object) {
        mCurrentView = (View)object;
    }
                                             
    public View getPrimaryItem() {
        return mCurrentView;
    }
}